<?php
require('base.php');
class mvc extends base{
	protected $request=array();
	protected $router=array();
	public function __construct($app){
		parent::__construct($app);
		$this->parse();
	}
	public function run(){
		$module=$this->controller($this->request['controller']);
		if(method_exists($module,$this->request['method'])){
			call_user_func(array($module,$this->request['method']));
		}else{
			$this->error('action not found.');
		}
	}
	private function parse(){
		if(!list($key,$val)=each($this->router)){$this->error('router not initialize!');}
		$c=$this->get('c',true);$a=$this->get('a',true);
		$this->request['controller']=$c?htmlspecialchars(trim($c)):$key;
		$this->request['method']=($c&&$a)?htmlspecialchars(trim($a)):key($val);
	}
	public function &controller($class){
		$this->tpl['tpl']=$this->itm['view'].$class.'/';
		return $this->load($this->sys['root'].$this->itm['controller'].$class.'/',$this->itm['prefix'].$class,array());
	}
	public function purview(&$router=array()){
		if(!empty($router)){$this->router=$router;}
		if(isset($this->router[$this->request['controller']])){
			if(isset($this->router[$this->request['controller']][$this->request['method']])){
				return true;
			}
		}
		return false;
	}
}
?>